package com.sdp.khy.baotou.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.khy.baotou.db.khy_notice.entity.KhyNotice;
import com.sdp.khy.baotou.db.khy_notice.service.IKhyNoticeService;
import com.sdp.khy.baotou.utils.CurrentLoginEmp;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-12-03
 */
@RestController
@RequestMapping("/KhyNoticeController")
public class KhyNoticeController {

	@Autowired
	private IKhyNoticeService iKhyNoticeService;
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	@Autowired
	private Query query;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iKhyNoticeService, KhyNotice.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody KhyNotice notice) {
		if(notice==null) {
			return Result.error("信息为空");
		}
		if(StringUtils.isEmpty(notice.getKeyId())) {
			JSONObject emp = currentLoginEmp.getCurrentLoginInfo();
			notice.setOpName(emp.getString("empName"));
			notice.setOpNo(emp.getString("empNo"));
			notice.setSendTime(LocalDateTime.now());
			iKhyNoticeService.save(notice);
		}else {
			notice.setSendTime(LocalDateTime.now());
			iKhyNoticeService.updateById(notice);
		}
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("删除的公告信息不完整");
		}
		
		iKhyNoticeService.removeById(keyId);
		
		return Result.ok();
	}
}

